package services;

import java.util.Iterator;

import objetos.CategoriaObj;
import objetos.ClienteObj;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

import classes.CartaoFidelidadeImpl;
import classes.CategoriaImpl;
import classes.ClienteImpl;

public class ClienteService {

	public OMElement efetuarCadastro(OMElement element) {
		
		ClienteImpl ci =  new ClienteImpl();
		
		// Cria elemento de retorno
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns_return = factory.createOMNamespace("return", "ret");
		OMElement return_element = factory.createOMElement("return", ns_return);
		
		// Objeto
		ClienteObj cliente= new ClienteObj();
		
		// Seta objeto
		cliente.loadOMElement(element);
		
		// Preenche retorno
		if (ci.efetuarCadastro(cliente)) return_element.setText("true");
		else return_element.setText("false");	
		
		return return_element;
	}
	
	public OMElement atualizarDadosCliente(OMElement element) {
		
		ClienteImpl ci = new ClienteImpl();
		
		// Cria elemento de retorno
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns_return = factory.createOMNamespace("return", "ret");
		OMElement return_element = factory.createOMElement("return", ns_return);
		
		// Objeto
		ClienteObj cliente = new ClienteObj();
		
		// Seta objeto
		cliente.loadOMElement(element);
		
		// Preenche retorno
		if ( ci.atualizarDadosCliente(cliente) ) return_element.setText("true");
		else return_element.setText("false");	
		
		return return_element;
	}
	
	public OMElement efetuarLogin(OMElement element) {
		
		ClienteImpl ci = new ClienteImpl();
		
		// Criar um OMElement que retorna um booleano
		Iterator<OMElement> it = element.getChildElements();
		OMElement login_par = it.next();
		OMElement senha_par = it.next();
		
		Boolean retorno = ci.efetuarLogin(login_par.getText(), senha_par.getText());
		
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns_return = factory.createOMNamespace("return", "ret");
		OMElement return_element = factory.createOMElement("return", ns_return);
		
		return_element.setText(retorno.toString());
		
		return return_element;
	}

}
